#include "homepage.h"
#include "ui_homepage.h"
#include <QDesktopServices>
#include <QUrl>
#include <QDir>
#include <QMenu>
#include <QMap>
#include "safepage.h"

QMenu* g_ctxMenu;
QPushButton* g_pBtn = NULL;
QMap<QString,QString> g_mpApps;
SafePage* g_PSafePage = NULL;

HomePage::HomePage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HomePage)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);
    ui->frame_1->show();
    ui->frame_2->hide();
    ui->frame_3->hide();

    ui->btnNew1->hide();
    ui->btnNew2->hide();
    ui->btnNew3->hide();
    ui->btnNew4->hide();
    ui->btnNew5->hide();

    g_ctxMenu = new QMenu(this);
    QAction* pAction = NULL;
    pAction = g_ctxMenu->addAction("新注册用户");
    connect(pAction, SIGNAL(triggered()), this, SLOT(on_new()));
    pAction = g_ctxMenu->addAction("老用户");
    connect(pAction, SIGNAL(triggered()), this, SLOT(on_old()));

    g_mpApps.insert("网易邮箱", "163.jpg");
    g_mpApps.insert("国美在线", "gmzx.jpg");
    g_mpApps.insert("携程", "xc.jpg");
    g_mpApps.insert("东方理财", "dflc.jpg");
    g_mpApps.insert("亚马逊", "ymx.jpg");

    g_PSafePage = new SafePage;
}

HomePage::~HomePage()
{
    delete ui;
}

void HomePage::on_btnDFCF_clicked()
{
    QString strMhtfile = qApp->applicationDirPath() + "/zl/dfcf.mht";
    QDesktopServices::openUrl(QUrl(strMhtfile));
}

void HomePage::on_btnTX_clicked()
{
    QString strMhtfile = qApp->applicationDirPath() + "/zl/tx.mht";
    QDesktopServices::openUrl(QUrl(strMhtfile));
}

void HomePage::on_btnXL_clicked()
{
    QString strMhtfile = qApp->applicationDirPath() + "/zl/xl.mht";
    QDesktopServices::openUrl(QUrl(strMhtfile));
}

void HomePage::popmenu(QObject* btn)
{
    g_pBtn = (QPushButton*)btn;
    QPoint pt = ((QPushButton*)btn)->pos();
    pt.setX( pt.x() + 0 );
    pt.setY( pt.y() + ((QPushButton*)btn)->height() );
    g_ctxMenu->popup(ui->frame_2->mapToGlobal(pt));
}


void HomePage::on_pushButton_2_clicked()
{
    popmenu(sender());
}

void HomePage::on_pushButton_4_clicked()
{
    popmenu(sender());
}

void HomePage::on_pushButton_20_clicked()
{
    popmenu(sender());
}

void HomePage::on_pushButton_21_clicked()
{
    popmenu(sender());
}

void HomePage::on_pushButton_22_clicked()
{
    popmenu(sender());
}

void HomePage::on_new()
{
    //网易邮箱
    if(g_pBtn == ui->pushButton_2){
        AddApp("网易邮箱", 1);
    }
    //国美在线
    else if(g_pBtn == ui->pushButton_4){
        AddApp("国美在线", 1);
    }
    //携程
    else if(g_pBtn == ui->pushButton_20){
        AddApp("携程", 1);
    }
    //东方理财
    else if(g_pBtn == ui->pushButton_21){
        AddApp("东方理财", 1);
    }
    //亚马逊
    else if(g_pBtn == ui->pushButton_22){
        AddApp("亚马逊", 1);
    }
    else{}
}

void HomePage::on_old()
{
    ui->frame_3->move(ui->frame_1->pos());
    ui->frame_1->hide();
    ui->frame_2->hide();
    ui->frame_3->show();
}

void HomePage::on_pushButton_23_clicked()
{
    AddApp(g_pBtn->text(), 0);
}


void HomePage::on_btnAdd_clicked()
{
    ui->frame_2->move(ui->frame_1->pos());
    ui->frame_1->hide();
    ui->frame_3->hide();
    ui->frame_2->show();
}

void HomePage::AddApp(QString strApp, int iNew)
{
    QPushButton* btnNew = NULL;
    if(!btnNew && ui->btnNew1->isHidden()) btnNew = ui->btnNew1;
    if(!btnNew && ui->btnNew2->isHidden()) btnNew = ui->btnNew2;
    if(!btnNew && ui->btnNew3->isHidden()) btnNew = ui->btnNew3;
    if(!btnNew && ui->btnNew4->isHidden()) btnNew = ui->btnNew4;
    if(!btnNew && ui->btnNew5->isHidden()) btnNew = ui->btnNew5;

    if(btnNew){
        bool bCanAdd = (g_mpApps.find(strApp) != g_mpApps.end());
        if(bCanAdd){
            QString strImage = g_mpApps.find(strApp).value();
            g_mpApps.remove(strApp);
            QPoint pt = ui->btnAdd->pos();
            ui->btnAdd->move(btnNew->pos());
            btnNew->move(pt);
            btnNew->setStyleSheet(QString("background-image: url(:/images/apps/%1);background-repeat:round,round;background-size:40px,60px;border:0px;")
                                  .arg(strImage));
            btnNew->show();
        }
    }

    ui->frame_1->show();
    ui->frame_2->hide();
    ui->frame_3->hide();

}

void HomePage::on_btnSafeSvcs_clicked()
{
    g_PSafePage->show();
}
